package com.emm.yixun.website.controller.marketing;

//框架包
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.emm.yixun.common.model.NeedReceiveRefund;
import com.emm.yixun.common.model.SalesGroup;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.estate.service.ProjectServiceFacade;
import com.emm.yixun.website.common.utils.base.Const;
import com.emm.yixun.website.common.utils.security.ShiroUtil;
import com.emm.yixun.website.constant.ConstantConfig;
import com.emm.yixun.website.controller.base.BaseController;
//website
//变化的引用

@Controller
@RequestMapping("/marketing/report/")
public class ReportController extends BaseController<NeedReceiveRefund> {
	@Autowired
	private ProjectServiceFacade projectServiceFacade;
	@Autowired
	private ConstantConfig constantConfig;

	/**
	 * 可售房源一览表
	 */
	@RequestMapping(value = "houseCanSaleSumReport")
	public String houseCanSaleSumReport(ModelMap map) {
		if (doSecurityIntercept(Const.RESOURCES_TYPE_MENU)) {
			// 查询菜单按钮权限
			User user = ShiroUtil.getCurrentUser();
			map.put("merchantId", user.getMerchantId());
			map.put("projectIds", getResult());
			map.put("reportUrl", constantConfig.getReportUrl());
			return "/marketing/report/houseCanSaleSumReport";
		}
		return Const.NO_AUTHORIZED_URL;
	}

	/**
	 * 存量房源统计分析表
	 */
	@RequestMapping(value = "precinctStockHouseAnalysisRepor")
	public String precinctStockHouseAnalysisRepor(ModelMap map) {
		if (doSecurityIntercept(Const.RESOURCES_TYPE_MENU)) {
			// 查询菜单按钮权限
			User user = ShiroUtil.getCurrentUser();
			map.put("merchantId", user.getMerchantId());
			map.put("projectIds", getResult());
			map.put("reportUrl", constantConfig.getReportUrl());
			return "/marketing/report/precinctStockHouseAnalysisRepor";
		}
		return Const.NO_AUTHORIZED_URL;
	}

	/**
	 * 成交房源统计分析表
	 */
	@RequestMapping(value = "soldHouseAnalysisRepor")
	public String soldHouseAnalysisRepor(ModelMap map) {
		if (doSecurityIntercept(Const.RESOURCES_TYPE_MENU)) {
			// 查询菜单按钮权限
			User user = ShiroUtil.getCurrentUser();
			map.put("merchantId", user.getMerchantId());
			map.put("projectIds", getResult());
			map.put("reportUrl", constantConfig.getReportUrl());
			return "/marketing/report/soldHouseAnalysisRepor";
		}
		return Const.NO_AUTHORIZED_URL;
	}

	/**
	 * 历年销售情况统计表
	 */
	@RequestMapping(value = "salesYearReport")
	public String salesYearReport(ModelMap map) {
		if (doSecurityIntercept(Const.RESOURCES_TYPE_MENU)) {
			// 查询菜单按钮权限
			User user = ShiroUtil.getCurrentUser();
			map.put("merchantId", user.getMerchantId());
			map.put("projectIds", getResult());
			map.put("reportUrl", constantConfig.getReportUrl());
			return "/marketing/report/salesYearReport";
		}
		return Const.NO_AUTHORIZED_URL;
	}

	/**
	 * 项目销售汇总表
	 */
	@RequestMapping(value = "salesSumForPrecinctReport")
	public String salesSumForPrecinctReport(ModelMap map) {
		if (doSecurityIntercept(Const.RESOURCES_TYPE_MENU)) {
			// 查询菜单按钮权限
			User user = ShiroUtil.getCurrentUser();
			map.put("merchantId", user.getMerchantId());
			map.put("projectIds", getResult());
			map.put("reportUrl", constantConfig.getReportUrl());
			return "/marketing/report/salesSumForPrecinctReport";
		}
		return Const.NO_AUTHORIZED_URL;
	}

	/**
	 * 项目销售排名情况表
	 */
	@RequestMapping(value = "salesRanking")
	public String salesRanking(ModelMap map) {
		if (doSecurityIntercept(Const.RESOURCES_TYPE_MENU)) {
			// 查询菜单按钮权限
			User user = ShiroUtil.getCurrentUser();
			map.put("merchantId", user.getMerchantId());
			map.put("projectIds", getResult());
			map.put("reportUrl", constantConfig.getReportUrl());
			return "/marketing/report/salesRanking";
		}
		return Const.NO_AUTHORIZED_URL;
	}

	/**
	 * 经纪人销售额排名报表
	 */
	@RequestMapping(value = "brokersSalesRanking")
	public String brokersSalesRanking(ModelMap map) {
		if (doSecurityIntercept(Const.RESOURCES_TYPE_MENU)) {
			// 查询菜单按钮权限
			User user = ShiroUtil.getCurrentUser();
			map.put("merchantId", user.getMerchantId());
			map.put("projectIds", getResult());
			map.put("reportUrl", constantConfig.getReportUrl());
			return "/marketing/report/brokersSalesRanking";
		}
		return Const.NO_AUTHORIZED_URL;
	}

	/**
	 * 项目存量时间统计表
	 */
	@RequestMapping(value = "projectStockTime")
	public String projectStockTime(ModelMap map) {
		if (doSecurityIntercept(Const.RESOURCES_TYPE_MENU)) {
			// 查询菜单按钮权限
			User user = ShiroUtil.getCurrentUser();
			map.put("merchantId", user.getMerchantId());
			map.put("projectIds", getResult());
			map.put("reportUrl", constantConfig.getReportUrl());
			return "/marketing/report/projectStockTime";
		}
		return Const.NO_AUTHORIZED_URL;
	}

	/**
	 * 项目去化率报表
	 */
	@RequestMapping(value = "projectRate")
	public String projectRate(ModelMap map) {
		if (doSecurityIntercept(Const.RESOURCES_TYPE_MENU)) {
			// 查询菜单按钮权限
			User user = ShiroUtil.getCurrentUser();
			map.put("merchantId", user.getMerchantId());
			map.put("projectIds", getResult());
			map.put("reportUrl", constantConfig.getReportUrl());
			return "/marketing/report/projectRate";
		}
		return Const.NO_AUTHORIZED_URL;
	}

	/**
	 * 项目销售日报详情表
	 */
	@RequestMapping(value = "projectSalesDaily")
	public String projectSalesDaily(ModelMap map) {
		if (doSecurityIntercept(Const.RESOURCES_TYPE_MENU)) {
			// 查询菜单按钮权限
			User user = ShiroUtil.getCurrentUser();
			map.put("merchantId", user.getMerchantId());
			map.put("projectIds", getResult());
			map.put("reportUrl", constantConfig.getReportUrl());
			return "/marketing/report/projectSalesDaily";
		}
		return Const.NO_AUTHORIZED_URL;
	}

	/**
	 * 项目应收款明细统计表
	 */
	@RequestMapping(value = "itemsReceivableDetail")
	public String itemsReceivableDetail(ModelMap map) {
		if (doSecurityIntercept(Const.RESOURCES_TYPE_MENU)) {
			// 查询菜单按钮权限
			User user = ShiroUtil.getCurrentUser();
			map.put("merchantId", user.getMerchantId());
			map.put("projectIds", getResult());
			map.put("reportUrl", constantConfig.getReportUrl());
			return "/marketing/report/itemsReceivableDetail";
		}
		return Const.NO_AUTHORIZED_URL;
	}

	/**
	 * 日志报表
	 */
	@RequestMapping(value = "sysLog")
	public String sysLog(ModelMap map) {
		if (doSecurityIntercept(Const.RESOURCES_TYPE_MENU)) {
			// 查询菜单按钮权限
			User user = ShiroUtil.getCurrentUser();
			map.put("merchantId", user.getMerchantId());
			// map.put("projectIds", getResult());
			map.put("reportUrl", constantConfig.getReportUrl());
			return "/marketing/report/sysLog";
		}
		return Const.NO_AUTHORIZED_URL;
	}
	
	/**
	 * 项目日报表
	 */
	@RequestMapping(value = "projectDayReport")
	public String projectDayReport(ModelMap map) {
		if (doSecurityIntercept(Const.RESOURCES_TYPE_MENU)) {
			// 查询菜单按钮权限
			User user = ShiroUtil.getCurrentUser();
			map.put("merchantId", user.getMerchantId());
			map.put("projectIds", getResult());
			map.put("reportUrl", constantConfig.getReportUrl());
			return "/marketing/report/projectDayReport";
		}
		return Const.NO_AUTHORIZED_URL;
	}
	
	/**
	 * 销售报表（周报/月报）
	 */
	@RequestMapping(value = "saleReport")
	public String saleReport(ModelMap map) {
		if (doSecurityIntercept(Const.RESOURCES_TYPE_MENU)) {
			// 查询菜单按钮权限
			User user = ShiroUtil.getCurrentUser();
			map.put("merchantId", user.getMerchantId());
			map.put("projectIds", getResult());
			map.put("reportUrl", constantConfig.getReportUrl());
			return "/marketing/report/saleReport";
		}
		return Const.NO_AUTHORIZED_URL;
	}
	
	/**
	 * 跟进方式统计
	 */
	@RequestMapping(value = "followReport")
	public String followReport(ModelMap map) {
		if (doSecurityIntercept(Const.RESOURCES_TYPE_MENU)) {
			// 查询菜单按钮权限
			User user = ShiroUtil.getCurrentUser();
			map.put("merchantId", user.getMerchantId());
			map.put("projectIds", getResult());
			map.put("reportUrl", constantConfig.getReportUrl());
			return "/marketing/report/followReport";
		}
		return Const.NO_AUTHORIZED_URL;
	}
	
	/**
	 * 客户分析表
	 */
	@RequestMapping(value = "customerAnalyticsReport")
	public String customerAnalyticsReport(ModelMap map) {
		if (doSecurityIntercept(Const.RESOURCES_TYPE_MENU)) {
			// 查询菜单按钮权限
			User user = ShiroUtil.getCurrentUser();
			map.put("merchantId", user.getMerchantId());
			map.put("projectIds", getResult());
			map.put("reportUrl", constantConfig.getReportUrl());
			return "/marketing/report/customerAnalyticsReport";
		}
		return Const.NO_AUTHORIZED_URL;
	}
	
	/**
	 * 库存房源分析表
	 */
	@RequestMapping(value = "houseStock")
	public String houseStock(ModelMap map) {
		if (doSecurityIntercept(Const.RESOURCES_TYPE_MENU)) {
			// 查询菜单按钮权限
			User user = ShiroUtil.getCurrentUser();
			map.put("merchantId", user.getMerchantId());
			map.put("projectIds", getResult());
			map.put("reportUrl", constantConfig.getReportUrl());
			return "/marketing/report/houseStock";
		}
		return Const.NO_AUTHORIZED_URL;
	}
	
	/**
	 * 库存房源明细分析表
	 */
	@RequestMapping(value = "houseStockIndust")
	public String houseStockIndust(ModelMap map) {
		if (doSecurityIntercept(Const.RESOURCES_TYPE_MENU)) {
			// 查询菜单按钮权限
			User user = ShiroUtil.getCurrentUser();
			map.put("merchantId", user.getMerchantId());
			map.put("projectIds", getResult());
			map.put("reportUrl", constantConfig.getReportUrl());
			return "/marketing/report/houseStockIndust";
		}
		return Const.NO_AUTHORIZED_URL;
	}

	public String getResult() {
		String projectIds = "";
		User user = ShiroUtil.getCurrentUser();
		ResponseList<SalesGroup> responseList = projectServiceFacade.findReportShiro(user.getId(), "menu_0009");
		if (responseList.isSuccess()) {
			List<SalesGroup> groups = responseList.getEntity();
			if (groups != null && groups.size() > 0) {
				projectIds += "(";
				for (int i = 0; i < groups.size(); i++) {
					projectIds += groups.get(i).getProjectId();
					if (i != groups.size() - 1) {
						projectIds += ",";
					}
				}
				projectIds += ")";
			} else if (groups == null && responseList.getResultMessage().equals("shiro")) {
				projectIds = "";
			} else {
				projectIds = "0";
			}
		} else {
			projectIds = "0";
		}
		return projectIds;
	}

}
